package com.zhou.controller;


import cn.hutool.http.server.HttpServerRequest;
import com.zhou.dto.admin.APIAdminDTO;
import com.zhou.dto.admin.AdminUpdateListDTO;
import com.zhou.result.R;
import com.zhou.service.AdminService;
import com.zhou.utils.APPApplicationException;
import com.zhou.utils.oss.AliOSSUtils;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description : 管理员模块
 * @Author : zyy
 * @Date: 2024/6/13  10:55
 */
@RestController
@RequestMapping("/admin")
@Slf4j
@Api(tags = "管理员模块")
public class AdminController {
    @Autowired
    private AdminService adminService;
/**     --------------------------------后台--------------------------------------------         */
    /**
     * 发送短信
     *
     * @param phone
     * @return
     */
    @Operation(summary = "发送短信")
    @PostMapping("code")
    public R code(String phone) {
        try {
            return R.success(adminService.code(phone));
        } catch (APPApplicationException e) {
            throw new APPApplicationException(e.getMessage());
        }
    }

    /**
     * 用户登录
     *
     * @param phone
     * @param code
     * @return
     * @throws
     */
    @Operation(summary = "用户验证码登录")
    @PostMapping("login")
    public R login(String phone, Integer code, HttpServletRequest request) {
        try {
            return R.success(adminService.login(phone, code,request));
        } catch (APPApplicationException e) {
            return R.error(e.getMessage());
        }
    }
    /**
     * 用户密码登录
     *
     * @param phone
     * @param password
     * @return
     * @throws
     */
    @Operation(summary = "用户密码登录")
    @PostMapping("login/password")
    public R login(String phone, String password,HttpServletRequest request) {
        try {
            return R.success(adminService.loginPassword(phone, password,request));
        } catch (APPApplicationException e) {
            return R.error(e.getMessage());
        }
    }
    /**
     * 用户登出
     *
     * @param phone
     * @return
     * @throws
     */
    @Operation(summary = "用户登出")
    @PostMapping("log_out")
    public R logOut(String phone) {
        try {
            return R.success(adminService.logOut(phone));
        } catch (APPApplicationException e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 分页查询
     */
    @Operation(summary = "分页查询")
    @GetMapping("query")
    public R query(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize
            , @RequestParam(required = false) Integer isDelFlag, @RequestParam(required = false) Integer isStatus) {
        try {
            return R.success(adminService.getList(page, pageSize, isDelFlag, isStatus));
        } catch (APPApplicationException e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改用户为管理员
     */
    @Operation(summary = "修改用户为管理员")
    @PutMapping("update")
    public R update(@RequestParam String id) {
        try {
            return R.success(adminService.update(id));
        } catch (APPApplicationException e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 管理员批量修改用户停用
     */
    @Operation(summary = "管理员批量修改用户停用")
    @PutMapping("update/list")
    public R updateListByStatus(@RequestBody AdminUpdateListDTO adminUpdateListDTO) {
        try {
            return R.success(adminService.adminUpdates(adminUpdateListDTO));
        } catch (APPApplicationException e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 导出excel
     */
    @Operation(summary = "导出excel")
    @GetMapping("export")
    public R export(@RequestParam(required = false) Integer isStatus, @RequestParam(required = false) Integer isDelFlag) {
        try {
            adminService.export(isStatus, isDelFlag);
            return R.success();
        } catch (APPApplicationException e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 导入excel
     */
    @Operation(summary = "导入excel")
    @PostMapping("input")
    public R input(@RequestPart @RequestParam("file") MultipartFile file) {
        try {
            adminService.input(file);
            return R.success();
        } catch (APPApplicationException e) {
            return R.error(e.getMessage());
        }
    }

    /**     --------------------------------小程序--------------------------------------------         */

    /**
     * 用户信息完善
     */
    @Operation(summary = "用户信息完善")
    @PutMapping("api/user")
    public R apiUpdateUser(@RequestBody APIAdminDTO adminDTO) {
        try {
            return R.success(adminService.apiUpdateUser(adminDTO));
        } catch (APPApplicationException e) {
            return R.error(e.getMessage());
        }
    }

    @Autowired
    private AliOSSUtils aliOSSUtils;

    /**
     * 用户上传头像
     */
    @Operation(summary = "用户上传头像")
    @PostMapping("api/user/upload")
    public R apiUpdateUser(@RequestPart @RequestParam("file") MultipartFile file) {
        try {
            log.info("开始上传" + file);
            String upload = aliOSSUtils.upload(file);
            log.info("上传成功");
            return R.success(upload);
        } catch (IOException e) {
            log.info("上传失败");
            return R.error(e.getMessage());
        }
    }
}
